package com.jy.api.service.impl;

import com.jy.api.dao.MemberAccountDao;
import com.jy.api.dao.MemberDao;
import com.jy.api.dao.MemberTeamAccountDao;
import com.jy.api.dao.TeamTransactionDao;
import com.jy.api.domain.Member;
import com.jy.api.domain.MemberAccount;
import com.jy.api.domain.MemberTeamAccount;
import com.jy.api.domain.TeamTransaction;
import com.jy.api.service.MemberTeamAccountService;
import com.jy.api.signUpReq.UpgradeMemberReq;
import com.jy.api.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by xiaohua on 2018/12/25.
 */
@Service
public class MemberTeamAccountServiceImpl implements MemberTeamAccountService {
    @Autowired
    private TeamTransactionDao transactionDao;
    @Autowired
    private MemberAccountDao memberAccountDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private MemberTeamAccountDao memberTeamAccountDao;

    @Override
    @Transactional
    public Result upgradeMember(UpgradeMemberReq req) {
        TeamTransaction transaction = transactionDao.findByStoreIdAndEnable(req.getStoreId(), true);

        double amount = 0.0;
        double bonus = 0.0;
        double points = 0.0;

        if (null == transaction) {
            return new Result(400, "该店还未成为连锁，暂不支持会员升级");
        } else {
            List<TeamTransaction> list = transactionDao.findByTeamIdAndEnable(transaction.getTeamId(), true);
            List<String> storeIds = new ArrayList<>();
            for (TeamTransaction t : list) {
                storeIds.add(t.getStoreId());
            }
            Member member = memberDao.findOne(req.getMemberId());
            Set<MemberAccount> memberAccounts = member.getAccounts();
            Set<MemberAccount> saveMemberAccount = new HashSet<>();
            for (MemberAccount account : memberAccounts) {
                if (account.isUse()) {
                    continue;
                } else {
                    for (String id : storeIds) {
                        if (StringUtils.equals(account.getStoreId(), id)) {
                            amount += account.getAmount();
                            bonus += account.getBonus();
                            points += account.getPoints();
                            account.setEnabled(false);
                            account.setUse(true);
                            account.setAmount(0.0);
                            account.setBonus(0.0);
                            account.setPoints(0);
                            saveMemberAccount.add(account);
                        }
                    }
                }

            }
            MemberTeamAccount ac = memberTeamAccountDao.findByMemberIdAndEnabledAndTeamId(req.getMemberId(), true, transaction.getTeamId());
            if (ac != null) {
                ac.setAmount(ac.getAmount() + amount);
                ac.setBonus(ac.getBonus() + bonus);
                ac.setPoints(ac.getPoints() + points);
            } else {
                ac = new MemberTeamAccount(amount, points, bonus, member.getId(), member.getName(), transaction.getTeamId());
            }
            memberTeamAccountDao.save(ac);
            memberAccountDao.save(saveMemberAccount);
            return new Result(200, "升级成功");
        }
    }
}
